﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace DataInterfaceDLL
{

    /// <summary>
    /// 杰瑞高清图片索引协议(V1.03)
    /// </summary>
    public class RecordParam
    {
        /// <summary>
        /// 杰瑞图片索引(V1.03)长度
        /// </summary>
        public const int cJARIINDEXCOUNT = 30;//
        private const string SHEN = "无京津晋冀蒙辽吉黑沪苏浙皖闽鲁豫鄂湘粤桂琼川贵云藏陕甘青宁渝军海空集北沈南兰广成济消水边电林通金警学特使领试港澳";
        
        /// <summary>
        /// 一组图片数量（不包括缩略图和车牌小图）,必须项，1,2,3
        /// </summary>
        public string SnapCount;
        /// <summary>
        /// 图片合成模式，必选项，
        /// 0-不合成，1-合成
        /// </summary>
        public string picJionMode;
        /// <summary>
        /// 记录类型,must,0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
        /// </summary>
        public string recordType;
        /// <summary>
        /// 数据提供厂家，must，我方（JARI），其他厂家按约定。
        /// </summary>
        public string Company;
        /// <summary>
        /// 采集设备,not must,0-HK200万摄像机，1-HK300万摄像机，2-HK500万摄像机，3-DH200万摄像机，4-DH300万摄像机，5-DH500万摄像机，6-HK130万球机，7-HK200万球机，8-DH130万球机，9DH200万球机
        /// </summary>
        public string captureDevice;
        /// <summary>
        /// 设备编号,must,采集设备的唯一标识编号
        /// </summary>
        public string deviceID;
        /// <summary>
        /// 采集人员,not must,当“3记录类型为”为“12监控抓拍时”指定采集人员用户名，没有设为空。
        /// </summary>
        public string captureUser;
        /// <summary>
        /// 违法行为代码,not must,本记录对应的违法行为代码，没有设为空。
        /// </summary>
        public string WFXWDM;
        /// <summary>
        /// 路口外部编号,must,路口的国标代码，如：101010
        /// </summary>
        public string GBDM;
        /// <summary>
        /// 部门编号,must,交警部门行政编号，如：3202000
        /// </summary>
        public string BMBH;
        /// <summary>
        /// 数据来源(只读)
        /// 1:闯红灯设备
        /// 2:公路卡口设备
        /// 3:测速设备
        /// 4:闭路电视
        /// 5:移动摄像
        /// 6:警务通
        /// 7:区间测速
        /// 8:卫星定位装置
        /// 9:其他电子设备
        /// 11:群众举报
        /// </summary>
        public string DATASOURCE
        {
            get
            {
                string ans = "";

                switch (recordType)
                {
                    case "0":
                        ans = "2";
                        break;
                    case "1":
                    case "2":
                        ans = "3";
                        break;
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                    case "10":
                    case "11":
                        ans = "1";
                        break;
                    case "12":
                        ans = "4";
                        break;
                    default:
                        ans = "9";
                        break;
                }

                return ans;
            }
        }
        /// <summary>
        /// 路口编号,must,任意长度的路口编号
        /// </summary>
        public string roadid;
        /// <summary>
        ///  路口名称，must，路口名称，不能含有_字符
        /// </summary>
        public string roadname;
        /// <summary>
        /// 方向,must,1-由东向西 2-由南向北 3-由西向东 4-由北向南
        /// </summary>
        public string directionID;
        /// <summary>
        /// 方向编号(只读)，1东->西，2西->东，3南->北，4北->南
        /// </summary>
        public string DIRID
        {
            get
            {
                string ans = "";
                switch (directionID)
                {
                    case "1":
                        ans = "1";
                        break;
                    case "2":
                        ans = "3";
                        break;
                    case "3":
                        ans = "2";
                        break;
                    case "4":
                        ans = "4";
                        break;
                    default:
                        ans = "";
                        break;
                }
                return ans;
            }
        }

        /// <summary>
        /// 友上方向(只读属性)方向：01-由东向西,02-由东向南,03-由东向北,04-由南向北,05-由南向西,06-由南向东,;07-由西向东,08-由西向北,09-由西向南,10-由北向南,11-由北向东,12-由北向西,13-进城,14-出城
        /// </summary>
        public uint YoSunDireaction
        {
            get
            {
                uint ans = 1u;
                switch (directionID)
                {
                    case "1":
                        ans = 1u;
                        break;
                    case "2":
                        ans = 4u;
                        break;
                    case "3":
                        ans = 7u;
                        break;
                    case "4":
                        ans = 10u;
                        break;
                    default:
                        ans = 1u;
                        break;
                }
                return ans;
            }
        }

        /// <summary>
        /// 友上日期(只读属性):从1970-01-01 08:00:00 到经过时间的秒数
        /// </summary>
        public uint YoSunDate
        {
            get
            { 
                DateTime t1 = DateTime.Parse(FormatDateTime);
                DateTime t2 = DateTime.Parse("1970-01-01 08:00:00");
                TimeSpan ts = t1.Subtract(t2);
                return (uint)ts.TotalSeconds;
            }
        }

        /// <summary>
        /// 友上省份代码(只读属性)
        /// </summary>
        public ushort YoSunPlate1
        {
            get
            {
                if (HPHM == "")
                {
                    return 0;
                }
                else if (HPHM.Contains("无"))
                {
                    return 0;
                }
                else
                {
                    return (ushort)(SHEN.IndexOf(HPHM.Substring(0, 1)));
                }

            }
        }

        /// <summary>
        /// 车牌后6位字母
        /// </summary>
        public string YoSunPlate2
        {
            get
            {
                if (HPHM == "")
                {
                    return "";
                }
                else if (HPHM.Contains("无"))
                {
                    return "";
                }
                else if (HPHM.Length == 7)
                {
                    return HPHM.Substring(1, 6);
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 1 蓝色，2 黑色，3 黄色，4 白色，
        /// </summary>
        public uint YoSunPlateColor
        {
            get
            {
                if (plateColor != "")
                {
                    if (plateColor.Contains("蓝"))
                    {
                        return 1u;
                    }
                    else if (plateColor.Contains("黑"))
                    {
                        return 2u;
                    }
                    else if (plateColor.Contains("黄"))
                    {
                        return 3u;
                    }
                    else if (plateColor.Contains("白"))
                    {
                        return 4u;
                    }

                }
                return 1u;

            }
        }
        /// <summary>
        /// 车道,must,由路口渠化由中间线到路边依次为1,2,3,4,5…
        /// </summary>
        public string CD;
        /// <summary>
        /// 经过时间，must，格式为：yyyyMMddHHmmssfff，一组图片中取识别车牌的那张图片时间
        /// </summary>
        public string JGSJ;
        /// <summary>
        /// 经过时间(只读)，must，格式为：yyyy-MM-dd HH:mm:ss，一组图片中取识别车牌的那张图片时间
        /// </summary>
        public string FormatDateTime
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return (JGSJ.Substring(0, 4) + "-" +
                        JGSJ.Substring(4, 2) + "-" +
                        JGSJ.Substring(6, 2) + " " +
                        JGSJ.Substring(8, 2) + ":" +
                            JGSJ.Substring(10, 2) + ":" +
                            JGSJ.Substring(12, 2));
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 年
        /// </summary>
        public string yyyy
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(0, 4);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 月
        /// </summary>
        public string mm
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(4, 2);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 日
        /// </summary>
        public string dd
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(6, 2);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 时
        /// </summary>
        public string hh
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(8, 2);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 分
        /// </summary>
        public string mi
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(10, 2);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 秒
        /// </summary>
        public string ss
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(12, 2);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 毫秒
        /// </summary>
        public string zzz
        {
            get
            {
                if ((JGSJ != null) && (JGSJ != "") && (JGSJ.Length == 17))
                {
                    return JGSJ.Substring(14, 3);
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 红灯开始时间,not must,格式为：yyyy-MM-dd hh:mm:ss，由检测器给出时间 
        /// </summary>
        public string RedLightBeginTime;
        /// <summary>
        /// 红灯持续时间，not must,单位秒，由检测器给出时间 
        /// </summary>
        public string RedLightDuration;
        /// <summary>
        /// 车牌类型
        /// 0---标准民用与军车车牌
        /// 1---02 式民用车牌
        /// 2---武警车牌
        /// 3---警车车牌
        /// 4---民用车双行尾牌
        /// </summary>
        public string plateType;
        /// <summary>
        /// 参考GA24.4（K11-大型普通客车，K21-中型普通客车，K31-小型普通客车，K33-轿车，H11-重型普通客车，H21-中型普通客车，M22-轻便二轮摩托车）
        /// </summary>
        public string HTAgentCLLX
        {
            get
            {
                if ((plateColor != null) && (plateColor != ""))
                {
                    if (plateColor.Contains("蓝"))
                    {
                        return "K33";
                    }
                    if (plateColor.Contains("黄"))
                    {
                        return "K11";
                    }
                }
                return "";
            }
        }

        /// <summary>
        /// 监控抓拍人工识别的车辆类型
        /// </summary>
        public string ManuelCarType;

        /// <summary>
        /// 车辆类型，01-大车02-小车 15挂车 16-教练车 23-警车
        /// </summary>
        public string carType
        {
            get
            {
                if (recordType == "12")
                {
                    return ManuelCarType;//人工识别的号牌类型
                }
                else
                {
                    //自动识别的号牌类型
                    if ((HPHM != null) && (HPHM != ""))
                    {
                        if (HPHM.Contains("挂"))
                        {
                            return "15";
                        }
                        if (HPHM.Contains("学"))
                        {
                            return "16";
                        }
                        if (HPHM.Contains("警"))
                        {
                            return "23";
                        }
                    }

                    if ((plateColor != null) && (plateColor != ""))
                    {
                        if (plateColor.Contains("蓝"))
                        {
                            return "02";
                        }
                        if (plateColor.Contains("黄"))
                        {
                            return "01";
                        }
                    }
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 参考GA24.7（如01-大型汽车，02-小型汽车，25-农机号牌，41-无号牌，42-假号牌，44-无法识别号牌种类），不能为空，不能使用99-其他号牌；
        /// </summary>
        public string HTAgentHPZL
        {
            get
            {
                if ((HPHM != null) && (HPHM != ""))
                {
                    if (HPHM.Contains("挂"))
                    {
                        return "15";
                    }
                    if (HPHM.Contains("学"))
                    {
                        return "16";
                    }
                    if (HPHM.Contains("警"))
                    {
                        return "23";
                    }
                }

                if ((plateColor != null) && (plateColor != ""))
                {
                    if (plateColor.Contains("蓝"))
                    {
                        return "02";
                    }
                    if (plateColor.Contains("黄"))
                    {
                        return "01";
                    }
                }
                return "41";
            }
        }
        /// <summary>
        /// 车牌颜色,not must,蓝 黄 白 黑，没有为空字符串
        /// </summary>
        public string plateColor;
        /// <summary>
        /// 0-白色，1-黄色，2-蓝色，3-黑色，4-绿色，9-其它颜色，不能为空；
        /// </summary>
        public string HTAgentHPYS
        {
            get
            {
                if (plateColor != "")
                {
                    if (plateColor.Contains("白"))
                    {
                        return "0";
                    }
                    else if (plateColor.Contains("黄"))
                    {
                        return "1";
                    }
                    else if (plateColor.Contains("蓝"))
                    {
                        return "2";
                    }
                    else if (plateColor.Contains("黑"))
                    {
                        return "3";
                    }
                    else if (plateColor.Contains("绿"))
                    {
                        return "4";
                    }

                }
                return "9";

            }
        }
        /// <summary>
        /// 车牌号，not must,车牌的汉字和大写字母以及数字部分，没有为空字符串
        /// </summary>
        public string HPHM;
        /// <summary>
        /// 无牌、未识别、无法识别均用半角“-”表示，其中无号牌要注意hpzl填41，无法识别号牌种类填44
        /// </summary>
        public string HTAgentHPHM
        {
            get
            {
                if ((HPHM == "") || (HPHM.Contains("无")))
                {
                    return "-";
                }
                else
                {
                    return HPHM;
                }
            }
        }
        /// <summary>
        /// 车身颜色，not must,0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色 255其他，没有为空字符串
        /// </summary>
        public string bodyColor;
        /// <summary>
        /// 限速，not must,无限速为空字符串
        /// </summary>
        public string speedLimit;
        /// <summary>
        /// 车速，not must,无车速为空字符串
        /// </summary>
        public string speed;
        /// <summary>
        /// 图片访问路径中固定部分（长度限定不能超过100个字节），如：FTP访问路径：ftp://user:666@192.168.1.1/001.jpg，其中图片路径为ftp://user:666@192.168.1.1/。
        /// </summary>
        public string HTAgentPic1Head;
        /// <summary>
        /// 图片访问路径中变化的部分（长度限定不能超过60个字节），如：FTP访问路径：ftp://user:666@192.168.1.1/001.jpg，其中图片名称为001.jpg。
        /// </summary>
        public string HTAgentPic1Tail;
        /// <summary>
        /// 通行图片，须为本地磁盘路径（“\”必须写为“\\”,如：D:\\Img\\1.jpg）
        /// </summary>
        public string HTAgentPic1FileFullName;
        /// <summary>
        /// 特征图片，须为本地磁盘路径（“\”必须写为“\\”,如：D:\\Img\\1.jpg）
        /// </summary>
        public string HTAgentTzPicFileFullName;
        /// <summary>
        /// 图片1路径，如http://172.16.22.2/卡口/某某路口/2014/05/09/6511-20140505101010-555-3-1.jpg，或者D:\DataRecv卡口图片\试验路口\2014\10\16\0121-20141016151726-267-Single.jpg
        /// </summary>
        public string PIC1;
        /// <summary>
        /// 图片2路径，如http://172.16.22.2/卡口/某某路口/2014/05/09/6511-20140505101010-555-3-2.jpg，或者D:\DataRecv卡口图片\试验路口\2014\10\16\0121-20141016151726-267-Single.jpg
        /// </summary>
        public string PIC2;
        /// <summary>
        /// 图片3路径，如http://172.16.22.2/卡口/某某路口/2014/05/09/6511-20140505101010-555-3-3.jpg，或者D:\DataRecv卡口图片\试验路口\2014\10\16\0121-20141016151726-267-Single.jpg
        /// </summary>
        public string PIC3;
        /// <summary>
        /// 图片4路径，备用
        /// </summary>
        public string PIC4;
        /// <summary>
        /// 压缩图片路径，如http://172.16.22.2/卡口/某某路口/2014/05/09/6511-20140505101010-555-1-1zoom.jpg，或者D:\DataRecv卡口图片\试验路口\2014\10\16\0121-20141016151726-267-Single.jpg
        /// </summary>
        public string PICZoom;
        /// <summary>
        /// 车牌图片路径，如http://172.16.22.2/卡口/某某路口/2014/05/09/6511-20140505101010-555-1-1plate.jpg，或者D:\DataRecv卡口图片\试验路口\2014\10\16\0121-20141016151726-267-Single.jpg
        /// </summary>
        public string PICPlate;
        /// <summary>
        /// 图片1数据
        /// </summary>
        public byte[] pic1Bytes;
        /// <summary>
        /// 图片2数据
        /// </summary>
        public byte[] pic2Bytes;
        /// <summary>
        /// 图片3数据
        /// </summary>
        public byte[] pic3Bytes;
        /// <summary>
        /// 图片4数据
        /// </summary>
        public byte[] pic4Bytes;
        /// <summary>
        /// 缩略/压缩 图片数据
        /// </summary>
        public byte[] picZoomBytes;
        /// <summary>
        /// 车牌图片数据
        /// </summary>
        public byte[] picPlateBytes;
        /// <summary>
        /// MQ发送的消息(只读)
        /// </summary>
        public string MSG
        {
            get
            {
                string msg = "";
                try
                {
                    //3_1_3_JARI_______01_实验室测试路口_2_2_20140523124215890___0_02_蓝_鲁QW195N_4_60_0.dat
                    //_http://172.16.22.2/PICSERVER/实验室测试路口/0122-20140523124215-890-3-1-Compress.jpg
                    //_http://172.16.22.2/PICSERVER/实验室测试路口/0122-20140523124215-890-3-2-Plate.jpg
                    //____http://172.16.22.2/PICSERVER/实验室测试路口/0122-20140523124215-890-3-2-Plate.jpg
                    string sp = "_";
                    msg = (SnapCount + sp +
                    picJionMode + sp +
                    recordType + sp +
                    Company + sp +
                    captureDevice + sp +
                    deviceID + sp +
                    captureUser + sp +
                    WFXWDM + sp +
                    GBDM + sp +
                    BMBH + sp +
                    roadid + sp +
                    roadname + sp +
                    directionID + sp +
                    CD + sp +
                    JGSJ + sp +
                    RedLightBeginTime + sp +
                    RedLightDuration + sp +
                    plateType + sp +
                    carType + sp +
                    plateColor + sp +
                    HPHM + sp +
                    bodyColor + sp +
                    speedLimit + sp +
                    speed + sp +
                    PIC1 + sp +
                    PIC2 + sp +
                    PIC3 + sp +
                    PIC4 + sp +
                    PICZoom + sp +
                    PICPlate);
                }
                catch
                { msg = ""; }
                return msg;
            }
        }
        /// <summary>
        /// 用于保存的索引文件名（只读）
        /// </summary>
        public string TXT
        {
            get
            {
                string sp = "_";
                string recordFileName = SnapCount + sp +
                    picJionMode + sp +
                    recordType + sp +
                    Company + sp +
                    captureDevice + sp +
                    deviceID + sp +
                    captureUser + sp +
                    WFXWDM + sp +
                    GBDM + sp +
                    BMBH + sp +
                    roadid + sp +
                    roadname + sp +
                    directionID + sp +
                    CD + sp +
                    JGSJ + sp +
                    RedLightBeginTime + sp +
                    RedLightDuration + sp +
                    plateType + sp +
                    carType + sp +
                    plateColor + sp +
                    HPHM + sp +
                    bodyColor + sp +
                    speedLimit + sp +
                    speed + ".dat";
                return recordFileName;
            }
        }
        /// <summary>
        /// 空构造函数
        /// </summary>
        public RecordParam()
        {
            SnapCount = "";
            picJionMode = "";
            recordType = "";
            Company = "";
            captureDevice = "";
            deviceID = "";
            captureUser = "";
            WFXWDM = "";
            GBDM = "";
            BMBH = "";
            roadid = "";
            roadname = "";
            directionID = "";
            CD = "";
            JGSJ = "";
            RedLightBeginTime = "";
            RedLightDuration = "";
            plateType = "";
            plateColor = "";
            HPHM = "";
            bodyColor = "";
            speedLimit = "";
            speed = "";
            PIC1 = "";
            PIC2 = "";
            PIC3 = "";
            PIC4 = "";
            PICZoom = "";
            PICPlate = "";
            ManuelCarType = "";
            pic1Bytes = null;
            pic2Bytes = null;
            pic3Bytes = null;
            pic4Bytes = null;
            picZoomBytes = null;
            picPlateBytes = null;
        }

        /// <summary>
        /// 杰瑞索引构造函数
        /// </summary>
        /// <param name="txt"></param>
        public RecordParam(string txt)
        {
            try
            {
                if (txt != "")
                {
                    string[] ss = txt.Split('_');
                    if ((ss != null) && (ss.Length == cJARIINDEXCOUNT))
                    {
                        SnapCount = ss[0];
                        picJionMode = ss[1];
                        recordType = ss[2];
                        Company = ss[3];
                        captureDevice = ss[4];
                        deviceID = ss[5];
                        captureUser = ss[6];
                        WFXWDM = ss[7];
                        GBDM = ss[8];
                        BMBH = ss[9];
                        roadid = ss[10];
                        roadname = ss[11];
                        directionID = ss[12];
                        CD = ss[13];
                        JGSJ = ss[14];
                        RedLightBeginTime = ss[15];
                        RedLightDuration = ss[16];
                        plateType = ss[17];
                        ManuelCarType = ss[18];
                        plateColor = ss[19];
                        HPHM = ss[20];
                        bodyColor = ss[21];
                        speedLimit = ss[22];
                        speed = ss[23];
                        PIC1 = ss[24];
                        PIC2 = ss[25];
                        PIC3 = ss[26];
                        PIC4 = ss[27];
                        PICZoom = ss[28];
                        PICPlate = ss[29];
                    }

                }

            }
            catch { }
        }

        /// <summary>
        /// 保存索引dat文件
        /// </summary>
        /// <param name="path">存储路径</param>
        public void Save(string path)
        {
            try
            {
                string splitor = "\\";
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string[] lines = new string[6];
                lines[0] = PIC1;
                lines[1] = PIC2;
                lines[2] = PIC3;
                lines[3] = PIC4;
                lines[4] = PICZoom;
                lines[5] = PICPlate;

                File.WriteAllLines(path + splitor + TXT, lines, Encoding.UTF8);
            }
            catch {  }

        }

        /// <summary>
        /// 保存失败图片
        /// </summary>
        /// <param name="path">根路径</param>
        /// <param name="saveby">按路口名称保存-按年月日保存-按年保存-按月保存-按日保存-按路口代码保存-按国标代码保存</param>
        public void SaveJpgFile(string path, string saveby)
        {
            try
            {
                if ((path != "") && (saveby != ""))
                {
                    string[] ss = saveby.Split('-');
                    if ((ss != null) && (ss.Length > 0))
                    {
                        string filepath = path;
                        foreach (string s in ss)
                        {
                            switch (s)
                            {
                                case "按路口名称保存":
                                    filepath = Path.Combine(filepath, roadname);
                                    break;
                                case "按年月日保存":
                                    filepath = Path.Combine(filepath, yyyy + mm + dd);
                                    break;
                                case "按年保存":
                                    filepath = Path.Combine(filepath, yyyy);
                                    break;
                                case "按月保存":
                                    filepath = Path.Combine(filepath, mm);
                                    break;
                                case "按日保存":
                                    filepath = Path.Combine(filepath, dd);
                                    break;
                                case "按路口代码保存":
                                    filepath = Path.Combine(filepath, roadid);
                                    break;
                                case "按国标代码保存":
                                    filepath = Path.Combine(filepath, GBDM);
                                    break;
                                default:
                                    break;
                            }
                        }
                        try
                        {
                            if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
                        }
                        catch { }

                        try
                        {
                            if ((pic1Bytes != null) && (PIC1 != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PIC1), pic1Bytes);
                        }
                        catch { }

                        try
                        {
                            if ((pic2Bytes != null) && (PIC2 != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PIC2), pic2Bytes);
                        }
                        catch { }

                        try
                        {
                            if ((pic3Bytes != null) && (PIC3 != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PIC3), pic3Bytes);
                        }
                        catch { }

                        try
                        {
                            if ((pic4Bytes != null) && (PIC4 != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PIC4), pic4Bytes);
                        }
                        catch { }

                        try
                        {
                            if ((picZoomBytes != null) && (PICZoom != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PICZoom), picZoomBytes);
                        }
                        catch { }

                        try
                        {
                            if ((picPlateBytes != null) && (PICPlate != "")) File.WriteAllBytes(filepath + "\\" + Path.GetFileName(PICPlate), picPlateBytes);
                        }
                        catch { }
                    }
                }
            }
            catch { }
        }
    }

    /// <summary>
    /// 杰瑞大数据平台卡口/电子警察数据接口
    /// </summary>
    public class UploadClient
    {


        private string m_ServerListenIP;
        private int m_ServerListenPort;

        private TcpClient m_Client;
        private Timer m_Timer;
        private NetworkStream m_ns;
        private BinaryReader m_br;
        private BinaryWriter m_bw;
        /// <summary>
        /// 是否连接到杰瑞卡电数据中间件
        /// </summary>
        public bool Connected
        {
            get
            {
                if ((m_Client != null) && (m_Client.Client != null) && (m_Client.Client.Connected))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 杰瑞大数据平台卡口/电子警察数据接口客户端构造函数
        /// </summary>
        /// <param name="ServerListenIP">服务端监听IP</param>
        /// <param name="ServerListenPort">服务端监听Port</param>
        public UploadClient(string ServerListenIP, int ServerListenPort)
        {
            try
            {
                m_ServerListenIP = ServerListenIP;
                m_ServerListenPort = ServerListenPort;
                m_Timer = new Timer(new TimerCallback(TimerCallbackFunc), null, 0, 10000);
            }
            catch { }
        }

        /// <summary>
        /// 尝试一次连接
        /// </summary>
        public void TryConnect()
        {
            try
            {
                if (m_Client == null)
                {
                    m_Client = new TcpClient();
                    m_Client.Connect(m_ServerListenIP, m_ServerListenPort);

                    if (m_Client.Client.Connected == true)
                    {
                        m_ns = m_Client.GetStream();
                        if (m_ns != null)
                        {
                            m_br = new BinaryReader(m_ns);
                            m_bw = new BinaryWriter(m_ns);
                        }
                    }
                }



                if (m_Client.Client.Connected == false)
                {
                    ShutDown();
                }
            }
            catch
            {
                ShutDown();
            }
        }

        private void TimerCallbackFunc(object obj)
        {
            TryConnect();
        }

        /// <summary>
        /// 上传卡口电警数据到杰瑞卡电数据中间件
        /// </summary>
        /// <param name="record">上传参数</param>
        /// <returns>true-上传成功，false-上传失败</returns>
        public bool UPLoad(RecordParam record)
        {
            bool ans = false;
            try
            {
                if ((record != null) && (m_Client != null) && (m_Client.Client != null) && (m_Client.Client.Connected == true) && (m_br != null) && (m_bw != null))
                {
                    string msg = record.MSG;
                    if (msg != "")
                        m_bw.Write(msg);
                    string ok = m_br.ReadString();
                    if (ok == (msg + "True"))
                    {
                        ans = true;
                    }
                    else
                    {
                        ans = false;
                    }
                }
            }
            catch { ans = false; }
            return ans;
        }

        /// <summary>
        /// 上传数据和记录到PicCenter
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public bool UPLoadWithBytes(RecordParam record)
        {
            bool ans = false;
            int zerolength = 0;

            string path1 = record.PIC1;//备份，准备失败后保存原路径
            string path2 = record.PIC2;
            string path3 = record.PIC3;
            string path4 = record.PIC4;
            string path5 = record.PICZoom;
            string path6 = record.PICPlate;

            try
            {
                if ((m_Client != null) && (m_Client.Client != null) && (m_Client.Client.Connected == true) && (m_br != null) && (m_bw != null))
                {
                    if (path1 != "") record.PIC1 = Path.GetFileName(path1);
                    if (path2 != "") record.PIC2 = Path.GetFileName(path2);
                    if (path3 != "") record.PIC3 = Path.GetFileName(path3);
                    if (path4 != "") record.PIC4 = Path.GetFileName(path4);
                    if (path5 != "") record.PICZoom = Path.GetFileName(path5);
                    if (path6 != "") record.PICPlate = Path.GetFileName(path6);

                    string msg = record.MSG;
                    if (msg != "") m_bw.Write(msg);


                    if ((record.PIC1 != "") && (record.pic1Bytes != null) && (record.pic1Bytes.Length > 0))
                    {
                        m_bw.Write(record.pic1Bytes.Length);
                        m_bw.Write(record.pic1Bytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    if ((record.PIC2 != "") && (record.pic2Bytes != null) && (record.pic2Bytes.Length > 0))
                    {
                        m_bw.Write(record.pic2Bytes.Length);
                        m_bw.Write(record.pic2Bytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    if ((record.PIC3 != "") && (record.pic3Bytes != null) && (record.pic3Bytes.Length > 0))
                    {
                        m_bw.Write(record.pic3Bytes.Length);
                        m_bw.Write(record.pic3Bytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    if ((record.PIC4 != "") && (record.pic4Bytes != null) && (record.pic4Bytes.Length > 0))
                    {
                        m_bw.Write(record.pic4Bytes.Length);
                        m_bw.Write(record.pic4Bytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    if ((record.PICZoom != "") && (record.picZoomBytes != null) && (record.picZoomBytes.Length > 0))
                    {
                        m_bw.Write(record.picZoomBytes.Length);
                        m_bw.Write(record.picZoomBytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    if ((record.PICPlate != "") && (record.picPlateBytes != null) && (record.picPlateBytes.Length > 0))
                    {
                        m_bw.Write(record.picPlateBytes.Length);
                        m_bw.Write(record.picPlateBytes);
                    }
                    else
                    {
                        m_bw.Write(zerolength);
                    }

                    string ok = m_br.ReadString();
                    if (ok == (msg + "True"))
                    {
                        ans = true;
                    }
                    else
                    {
                        ans = false;
                    }
                }
            }
            catch
            {
                ans = false;
            }
            finally
            {
                record.PIC1 = path1;
                record.PIC2 = path2;
                record.PIC3 = path3;
                record.PIC4 = path4;
                record.PICZoom = path5;
                record.PICPlate = path6;
            }
            return ans;
        }

        private void ShutDown()
        {
            try
            {
                if (m_br != null)
                {
                    m_br.Close();
                    m_br = null;
                }
                if (m_bw != null)
                {
                    m_bw.Close();
                    m_bw = null;
                }
                if (m_ns != null)
                {
                    m_ns.Close();
                    m_ns = null;
                }
                if (m_Client != null)
                {
                    if (m_Client.Client != null)
                    {
                        if (m_Client.Client.Connected)
                        {
                            m_Client.Client.Shutdown(SocketShutdown.Both);
                            m_Client.Client.Close();
                        }
                    }
                    m_Client.Close();
                    m_Client = null;
                }
            }
            catch { }
        }

        /// <summary>
        /// 关闭，销毁资源
        /// </summary>
        public void Close()
        {
            ShutDown();
            try
            {
                if (m_Timer != null)
                {
                    m_Timer.Dispose();
                }
            }
            catch { }
        }
    }
}
